var orgall = null;//所有人员
var userall = null;//所有单位
var operation_state = null;//项目状态数据
var category_type = null;//项目业务类型数据
var progect_type = null;//项目业务类型数据
$(function () {
    $.when(
        vm.init()
    ).done(
        $("#jqGrid").jqGrid({
            url: baseURL + 'bss/apbase/tkdbxmlist',
            datatype: "json",
            colModel: [
                {label: 'guid', name: 'guid', index: 'guid', width: 50, key: true, hidden: true},
               {label: '项目名称', name: 'pBName', index: 'p_b_name', width: 200},
               {
                label: '项目单位', name: 'pBOrgid', index: 'p_b_orgid', width: 80,
                    formatter: function (cellvalue, options, rowObject) {

                        for (d in orgall) {
                            if (cellvalue == vm.orgall[d].deptId)
                                return orgall[d].name;
                        }
                    }
                },


                {
                    label: '参与人', name: 'pBCollectid', index: 'p_b_collectid', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        var names = "";


                        var ss = (cellvalue.replace(/\"/g, "").replace("[", "").replace("]", "") + "").split(",");
                        for (var i = 0; i < ss.length; i++) {

                            for (d in userall) {
                                if (ss[i] == userall[d].userId + "") {

                                    names += userall[d].username + ",";
                                }
                            }
                        }
                        if (names.length > 0) {
                            names = names.substring(0, names.length - 1);
                        }
                        return names;
                    }
                },
                {
                    label: '业务类型', name: 'pBCategory', index: 'p_b_category', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in category_type) {
                            if (cellvalue == category_type[d].dKey) return category_type[d].dValue;
                        }
                    }
                },

                {
                    label: '项目状态', name: 'pOperationState', index: 'p_operation_state', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in operation_state) {
                            if (cellvalue == operation_state[d].dKey) return operation_state[d].dValue;
                        }
                    }
                },



                {label: '项目地址', name: 'pBAddress', index: 'p_b_address', width: 100},
                {
                    label: '创建时间', name: 'pBAddtime', index: 'p_b_addtime', width: 80,
                    formatter: function (value, options, row) {
                        return value != null ? new Date(value).Format('yyyy-MM-dd hh:mm') : "";
                    }
                },
                {
                    label: '操作', name: '', width: 110,
                    formatter: function (cellvalue, options, rowObject) {
                     if (rowObject.pOperationState == "101"||rowObject.pOperationState == "102") {
                         return "<a  class='btn btn-primary' onclick='showother(\"" + rowObject.guid + "\",\"" + rowObject.pBType + "\" )' >&nbsp;项目信息</a>&nbsp;&nbsp;";
                      }
                      else if (rowObject.pOperationState == "105")
                        {
                            return "<a  class='btn btn-primary' onclick='showmsg(\"" + rowObject.pAuditOpinion + "\" )' >&nbsp;退回意见</a>&nbsp;&nbsp;";

                        }
                        else
                         {
                             return "";
                         }


                    }
                }


            ],
            viewrecords: true,
            height: 380,
            rowNum: 4,
            rowList: [10, 30, 50],
            rownumbers: true,
            rownumWidth: 25,
            autowidth: true,
            multiselect: true,
            pager: "#jqGridPager",
            jsonReader: {
                root: "page.list",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount"
            },
            prmNames: {
                page: "page",
                rows: "limit",
                order: "order"
            },
            gridComplete: function () {
                //隐藏grid底部滚动条
                $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            }
        })
    );


});



function showother(guid, ptype) {
     var title = "项目信息";
    var content = "apbaseothorall.html?guid=" + guid + "&ptype=" + ptype;
    layer.open({
        type: 2,
        title: false,
        area: ['95%', '95%'],
        content: content

    });
}

function showmsg(msess) {
    layer.open({
        type: 1
        ,title: false //不显示标题栏
        ,closeBtn: false
        ,area: '500px;'
        ,shade: 0.8
        ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
        ,resize: false
        ,btn: ['关闭']
        ,btnAlign: 'c'
        ,moveType: 1 //拖拽模式，0或者1
        ,content: "<div style='padding: 20px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 500;'>"+msess+"</div>"

    });

}


var vm = new Vue({
    el: '#rrapp',

    data: {
        q: {
            pBType: null,
            pBName: null,
            pBState: null
        },

        orgall: [],
        userall: [],
        userallmy:null,
        showList: true,

        title: null,
        aPBase: {pBCollectid: []},
        category_type: [],
        operation_state:[],
        progect_type:[],
        fw:[],
        bb:[],


    },
    methods: {

        init: function () {

            $.get(baseURL + "api/bdatadictionary/select?d_code=operation_state" , function (r) {
                operation_state = r.returnlist;
                vm.operation_state = operation_state;
               });
            $.get(baseURL + "api/bdatadictionary/select?d_code=category_type" , function (r) {
                category_type= r.returnlist;
                vm.category_type = category_type;
            });
            $.get(baseURL + "api/bdatadictionary/select?d_code=progect_type" , function (r) {
                progect_type =progect_type
                vm.progect_type = r.returnlist;
            });

            $.get(baseURL + "sys/dept/selectall", function (r) {
                orgall = r.returnlist;
                vm.orgall = orgall;
            });
            $.get(baseURL + "sys/user/select", function (r) {
                userall = r.returnlist;
                vm.userall = userall;
            });
            $.get(baseURL + "api/bdatadictionary/select?d_code=project_scope" , function (r) {

                vm.fw = r.returnlist;
            });
            $.get(baseURL + "api/bdatadictionary/select?d_code=edition_type" , function (r) {

                vm.bb = r.returnlist;
            });

            },
        query: function () {
            vm.reload();
        },
        add: function () {

            vm.showList = false;
            vm.title = "新增";
            vm.aPBase = {pBCollectid: []},
            // vm.category_type = category_type;
            // vm.orgall=orgall;
            // vm.userall=userall;
            // vm. progect_type=progect_type;
            // vm. fw=[100,500,1000];
            // vm. bb=["国网","南网","地电"];
           $("#selectorg").val(orgall[0]);
            var userallmy = [];
            for(var i=0 ;i<userall.length;i++)
            {
                if(userall[i].deptId== orgall[0].deptId)
                {
                    userallmy.push(userall[i]);
                }
            }
            vm.userallmy=userallmy;
        },
        deptTree: function (){
           var ids = $("#selectorg").val();
            var userallmy = [];
            for(var i=0 ;i<userall.length;i++)
            {
                if(userall[i].deptId==ids)
                {
                    userallmy.push(userall[i]);
                }
            }
            vm.userallmy=userallmy;
        },
        update: function (event) {
            var guid = getSelectedRow();
            if (guid == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(guid);
            // vm.category_type = category_type;
            // vm.orgall=orgall;
            // vm.userall=userall;
            // vm. progect_type=progect_type;
            // vm. fw=[100,500,1000];
            // vm. bb=["国网","南网","地电"];



        },
        saveOrUpdate: function (event) {
            var url = vm.aPBase.guid == null ? "bss/apbase/save" : "bss/apbase/update";

            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.aPBase),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var guids = getSelectedRows();
            if (guids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "bss/apbase/delete",
                    contentType: "application/json",
                    data: JSON.stringify(guids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },

        getInfo: function (guid) {
            $.get(baseURL + "bss/apbase/info/" + guid, function (r) {
                vm.aPBase = r.aPBase;
                vm.aPBase.pBCollectid = r.aPBase.pBCollectid.replace(/\"/g, "").replace("[", "").replace("]", "").split(",");
                vm.userallmy = r.userallmy;
                var i = 0;
                var item = new Array();
                for (d in vm.progect_type) {

                    if (vm.progect_type[d].dKey == r.aPBase.pBType) {

                        item[i] = vm.progect_type[d];
                        i++;
                    }
                }

            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'pBType': vm.q.pBType,
                    'pBName': vm.q.pBName,
                    'pBState': vm.q.pBState

                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});